import R from "ramda"

// curried style
const add = (x: number) => (y: number) => x + y

let three = add(1)(2)
console.log(three)

// partial function
const plus = (x: number, y: number) => x + y;
const inc = R.curry(plus)(1)
const four = inc(three)
console.log(four)

// Inplementation of curry and uncurry
const curry = <A, B, C>(f: (a: A, b: B) => C) =>
    (a: A) => (b: B) => f(a, b)
const uncurry = <A, B, C>(f: (a: A) => (b: B) => C) =>
    (a: A, b: B) => f(a)(b)


// Trace
const traceWithLabel = <T>(label: string, value: T) => {
    console.log(`${label}:${value}`);
    return value;
}
const trace = R.curry(traceWithLabel)

const msd = R.compose(
    trace("sqrt"),
    Math.sqrt,
    trace("sum"),
    R.sum,
    trace("map square"),
    R.map((x: number) => x * x),
)

const arr: number[] = [1, 2, 3, 4, 5]
const dist = msd(arr)
console.log(dist)

// flip
const info = (v: number, info: string) => `${info}:${v}`
//(f:(a,b)=>c) => (b,a)=>c
const flip = <A, B, C>(f: (a: A, b: B) => C) => (b: B, a: A) => f(a, b)
const f_info = curry(flip(info))


console.log(f_info("value is")(10))